package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Account;
import org.csu.secondhand.api.entity.Posting;
import org.csu.secondhand.api.service.AccountService;
import org.csu.secondhand.api.service.PostingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/posting")
public class PostingController {

    @Autowired
    private PostingService postingService;
    @Autowired
    private AccountService accountService;

    @GetMapping("getAllPosting")
    @ResponseBody
    public Map<Object, Object> getAllPosting() {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = postingService.getAllPosting();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", postingList);
        return map;
    }

    @GetMapping("getMyPosting")
    @ResponseBody
    public Map<Object, Object> getMyPosting(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = postingService.getMyPosting(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", postingList);
        return map;
    }

    @GetMapping("getThoseILike")
    @ResponseBody
    public Map<Object, Object> getThoseILike(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = postingService.getThoseILike(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", postingList);
        return map;
    }

    @GetMapping("getThoseIComment")
    @ResponseBody
    public Map<Object, Object> getThoseIComment(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = postingService.getThoseIComment(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", postingList);
        return map;
    }

    @GetMapping("searchPosting")
    @ResponseBody
    public Map<Object, Object> searchPosting(String keyword) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = postingService.searchPosting(keyword);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", postingList);
        return map;
    }

    @GetMapping("getPostingById")
    @ResponseBody
    public Map<Object, Object> getPostingById(long id) {
        Map<Object, Object> map = new HashMap<>();
        Posting posting = postingService.getPostingById(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", posting);
        return map;
    }

    @PostMapping("deletePosting")
    @ResponseBody
    public Map<Object, Object> deletePosting(long id) {
        Map<Object, Object> map = new HashMap<>();
        Posting posting = postingService.deletePosting(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", posting);
        return map;
    }

    @PostMapping("insertPosting")
    @ResponseBody
    public Map<Object, Object> insertPosting(String username,
                                             String content,
                                             String photo) {
        String tag = "";
        while(content.contains("#") & content.contains(" ")) {
            int i1 = content.indexOf("#");
            int i2 = content.indexOf(" ");
            String temp = content.substring(i1, i2);
            content = content.substring(i2+1);
            if(tag.equals("")) {
                tag += temp;
            }
            else {
                tag = tag + "`" + temp;
            }
        }
        Map<Object, Object> map = new HashMap<>();
        Posting posting = postingService.insertPosting(
                new Posting(0, TimeUtil.getTime(), username, content, photo, 0, 0, tag));
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", posting);
        return map;
    }

    @PostMapping("editPosting")
    @ResponseBody
    public Map<Object, Object> editPosting(long id,
                                           String username,
                                           String content,
                                           String photo) {
        String tag = "";
        while(content.contains("#") & content.contains(" ")) {
            int i1 = content.indexOf("#");
            int i2 = content.indexOf(" ");
            String temp = content.substring(i1, i2);
            content = content.substring(i2+1);
            if(tag.equals("")) {
                tag += temp;
            }
            else {
                tag = tag + "`" + temp;
            }
        }
        Map<Object, Object> map = new HashMap<>();
        Posting posting = postingService.editPosting(
                new Posting(id, TimeUtil.getTime(), username, content, photo, 0, 0, tag));
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", posting);
        return map;
    }

    @GetMapping("getPostingUserHeadImg")
    @ResponseBody
    public Map<Object, Object> getPostingUserHeadImg(int type, String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = new ArrayList<>();
        if(type == 0) {
            postingList = postingService.getAllPosting();
        }
        if(type == 1) {
            postingList = postingService.getMyPosting(user);
        }
        if(type == 2) {
            postingList = postingService.getThoseILike(user);
        }
        if(type == 3) {
            postingList = postingService.getThoseIComment(user);
        }
        List<String> headImg = new ArrayList<>();
        for (int i = 0; i < postingList.size(); i++) {
            Account account = accountService.getAccount(postingList.get(i).getUsername());
            headImg.add(account.getHeadimg());
        }
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", headImg);
        return map;
    }

    @GetMapping("getPostingUserName")
    @ResponseBody
    public Map<Object, Object> getPostingUserName(int type, String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = new ArrayList<>();
        if(type == 0) {
            postingList = postingService.getAllPosting();
        }
        if(type == 1) {
            postingList = postingService.getMyPosting(user);
        }
        if(type == 2) {
            postingList = postingService.getThoseILike(user);
        }
        if(type == 3) {
            postingList = postingService.getThoseIComment(user);
        }
        List<String> nameList = new ArrayList<>();
        for (int i = 0; i < postingList.size(); i++) {
            Account account = accountService.getAccount(postingList.get(i).getUsername());
            nameList.add(account.getUsername());
        }
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", nameList);
        return map;
    }
}
